using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TmxLib.Controls
{
    class TreeViewWithRightButtonController
    {
        private TreeView _tree;
        private Image _image;


        public TreeViewWithRightButtonController(TreeView tree, Image image)
        {
            _tree = tree;
            _image = image;

            _tree.DrawMode = TreeViewDrawMode.OwnerDrawText;
            _tree.DrawNode += new DrawTreeNodeEventHandler(Tree_DrawNode);
            _tree.MouseDown += new MouseEventHandler(Tree_MouseDown);
            _tree.MouseMove += new MouseEventHandler(Tree_MouseMove);
        }

        void Tree_MouseMove(object sender, MouseEventArgs e)
        {
        }

        void Tree_MouseDown(object sender, MouseEventArgs e)
        {
        }

        void Tree_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            try
            {
                // e.Bounds - node bounds
                Graphics graphics2 = e.Graphics;
                TreeNode node1 = e.Node;
                Rectangle rectangle2 = node1.Bounds;
                Size size1 = TextRenderer.MeasureText(node1.Text, node1.TreeView.Font);
                Point point1 = new Point(rectangle2.X - 1, rectangle2.Y);
                rectangle2 = new Rectangle(point1, new Size(size1.Width, rectangle2.Height));
                // DrawTreeNodeEventArgs args2 = new DrawTreeNodeEventArgs(graphics2, node1, rectangle2, (TreeNodeStates)nmtvcustomdraw1.nmcd.uItemState);
                // this.OnDrawNode(args2);

                // draw default
                TreeNodeStates states1 = e.State;
                Font font1 = (node1.NodeFont != null) ? node1.NodeFont : node1.TreeView.Font;
                Color color3 = (((states1 & TreeNodeStates.Selected) == TreeNodeStates.Selected) && node1.TreeView.Focused) ? SystemColors.HighlightText : ((node1.ForeColor != Color.Empty) ? node1.ForeColor : node1.TreeView.ForeColor);
                if ((states1 & TreeNodeStates.Selected) == TreeNodeStates.Selected)
                {
                    graphics2.FillRectangle(SystemBrushes.Highlight, rectangle2);
                    ControlPaint.DrawFocusRectangle(graphics2, rectangle2, color3, SystemColors.Highlight);
                    TextRenderer.DrawText(graphics2, e.Node.Text, font1, rectangle2, color3, TextFormatFlags.GlyphOverhangPadding | TextFormatFlags.VerticalCenter);
                }
                else
                {
                    bool isSelected = e.Node.TreeView.SelectedNode == e.Node;
                    Brush b =
                        isSelected
                            ? new SolidBrush(GetSelectedColor())
                            : SystemBrushes.Window;

                    graphics2.FillRectangle(SystemBrushes.Window, rectangle2);

                    if (isSelected)
                    {
                        Rectangle r3 = rectangle2;
                        r3.Inflate(0, -1);
                        graphics2.FillRectangle(b, rectangle2);
                    }

                    TextRenderer.DrawText(graphics2, e.Node.Text, font1, rectangle2, color3, TextFormatFlags.GlyphOverhangPadding | TextFormatFlags.VerticalCenter);
                }
            }
            finally
            {
            }

        }

        Color GetSelectedColor()
        {
            Color c = SystemColors.Window;
            float f = 0.9f;
            return Color.FromArgb(
                (int)(c.R * f),
                (int)(c.G * f),
                (int)(c.B * f));
        }

    }
}
